import {read_text} from "./utils.js";
import {playAlarm,stopAlarm} from "./alarmAudio.js";
export class artificialAlarm{
    isReading;
    isStopped;
    readingId;
    readingContent;
    constructor() {
        this.isReading = false;
        this.isStopped = true;
        this.readingContent = {fire:[],smoke:[]};
    }
    singleReading(){
        if(this.readingContent['fire'].length===0&&this.readingContent['smoke'].length===0 || this.isStopped){
            // 没有内容播报，中止
            clearInterval(this.readingId);
            this.isReading = false;
            let text = "";
            if(this.isStopped){
                text = "停止摄像头采样与报警！";
                this.readingContent = {fire:[],smoke:[]}
            }else{
                text = "暂时没有危急情况，即将停止播报";
            }
            console.log(text);
            read_text(text);
            stopAlarm();
        }else{
            playAlarm();
            let content = `警告！警告！`;
            if(this.readingContent['fire'].length>0){
                for(let i=0;i<this.readingContent['fire'].length;i++){
                    content += this.readingContent['fire'][i]+"。";
                }
                content += "发起火焰报警。";
            }
            if(this.readingContent['smoke'].length>0){
                for(let i=0;i<this.readingContent['smoke'].length;i++){
                    content += this.readingContent['smoke'][i]+"。";
                }
                content += "发起烟雾报警。";
            }
            console.log(content);
            read_text(content);
        }
    }
    startReading(){
        if(!this.isReading&&(this.readingContent['fire'].length!==0||this.readingContent['smoke'].length!==0)){
            this.isReading = true;
            console.log("开始报警");
            this.singleReading();
            this.readingId = setInterval(()=>{
                this.singleReading();
            },15000);
        }else{
            console.log("不满足报警条件");
            console.log(`isReading:${this.isReading},length:${this.readingContent['fire'].length},${this.readingContent['smoke'].length}`)
        }

    }
}

function singleReading(){
    if(readingContent['fire'].length===0&&readingContent['smoke'].length===0 || isStopped){
        // 没有内容播报，中止
        clearInterval(readingId);
        isReading = false;
        let text = "";
        if(isStopped){
            text = "停止摄像头采样与报警！";
            readingContent = {fire:[],smoke:[]}
        }else{
            text = "暂时没有危急情况，即将停止播报";
        }
        console.log(text);
        read_text(text);
        stopAlarm();
    }else{
        playAlarm();
        let content = `警告！警告！`;
        if(readingContent['fire'].length>0){
            for(let i=0;i<readingContent['fire'].length;i++){
                content += readingContent['fire'][i]+"。";
            }
            content += "发起火焰报警。";
        }
        if(readingContent['smoke'].length>0){
            for(let i=0;i<readingContent['smoke'].length;i++){
                content += readingContent['smoke'][i]+"。";
            }
            content += "发起烟雾报警。";
        }
        console.log(content);
        read_text(content);
    }
}
export function startReading(){
    if(!isReading&&(readingContent['fire'].length!==0||readingContent['smoke'].length!==0)){
        isReading = true;
        console.log("开始报警");
        singleReading();
        readingId = setInterval(()=>{
            singleReading();
        },15000);
    }else{
        console.log("不满足报警条件");
        console.log(`isReading:${isReading},length:${readingContent['fire'].length},${readingContent['smoke'].length}`)
    }

}